package com.es.hr.baoxiaoShenpi;

import java.lang.reflect.Field;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import com.es.business.area.AreaBean;
import com.es.business.area.AreaBiz;
import com.es.business.city.CityBean;
import com.es.business.city.CityBiz;
import com.es.business.citys.CitysBean;
import com.es.business.citys.CitysBiz;
import com.es.business.province.ProvinceBean;
import com.es.business.province.ProvinceBiz;
import com.es.common.CommonBean;
import com.es.common.CommonBiz;
import com.es.hr.baoxiao.BaoxiaoBean;
import com.es.hr.baoxiao.BaoxiaoBiz;
import com.es.hr.emp.EmpInfoBean;
import com.es.main.LoginUserBeen;
import com.es.util.page.PageBean;
import com.opensymphony.webwork.interceptor.ServletRequestAware;
import com.opensymphony.xwork.ActionSupport;

public class SearchBaoxiaoShenpiHistoryDoAction extends ActionSupport
implements ServletRequestAware{
	 HttpServletRequest request;

	public void setServletRequest(HttpServletRequest paramHttpServletRequest) {
		// TODO Auto-generated method stub
		this.request = paramHttpServletRequest;
	}
	
	public String execute() throws Exception{
		String result = "";
		String message = "";
		BaoxiaoShenpiBiz baoxiaoShenpiBiz = new BaoxiaoShenpiBiz();
		BaoxiaoBiz baoxiaoBiz = new BaoxiaoBiz();
		
		LoginUserBeen userLogin = (LoginUserBeen)request.getSession().getAttribute("loginuser");
		String role = userLogin.getLoginRole();
		
		Date date = new Date();
		int year = date.getYear() + 1900;
		int mouth = date.getMonth() + 1;
		int day = date.getDate();
		
		String syear = new Integer(year).toString();
		String smouth = new Integer(mouth).toString();
		if(smouth.length() < 2) smouth = "0" + smouth;
		String sday = new Integer(day).toString();
		if(sday.length() < 2) sday = "0" + sday;
		
		String baoxiao_date = syear + "-" + smouth + "-" + sday;
		this.request.setAttribute("baoxiaoStateList", new CommonBiz().getCommonByType("baoxiaostate"));
		
		String area_code = this.request.getParameter("emp_area_code");
		this.request.setAttribute("emp_area_code", area_code);
		
		String province_code = this.request.getParameter("emp_province_code");
		this.request.setAttribute("emp_province_code", province_code);
		String city_code = this.request.getParameter("emp_city_code");
		this.request.setAttribute("emp_city_code", city_code);
		
		String rtm_code = this.request.getParameter("rtm_code");
		String rtm_name = this.request.getParameter("rtm_name");
		String ctm_code = this.request.getParameter("ctm_code");
		String ctm_name = this.request.getParameter("ctm_name");
		
		this.request.setAttribute("rtm_code", rtm_code);
		this.request.setAttribute("rtm_name", rtm_name);
		this.request.setAttribute("ctm_code", ctm_code);
		this.request.setAttribute("ctm_name", ctm_name);
		
		String baoxiao_starttime = this.request.getParameter("baoxiao_starttime").trim();
		this.request.setAttribute("baoxiao_starttime", baoxiao_starttime);
		if(baoxiao_starttime.length() > 0) baoxiao_starttime += "-01";
		String baoxiao_endtime = this.request.getParameter("baoxiao_endtime").trim();
		this.request.setAttribute("baoxiao_endtime", baoxiao_endtime);
		if(baoxiao_endtime.length() > 0) baoxiao_endtime += "-32";
		
		String baoxiao_state = this.request.getParameter("baoxiao_state");
		this.request.setAttribute("baoxiao_state", baoxiao_state);
		int curpage = this.request.getParameter("curpage") == null?1:new Integer(this.request.getParameter("curpage")).intValue();
		this.request.setAttribute("curpage", curpage);
		
		this.request.setAttribute("areaList", new com.es.util.common.CommonBiz(userLogin).getAreaList());

		if(area_code.length() > 0){
			this.request.setAttribute("provinceList", new ProvinceBiz().getProvinceByAreaCode(area_code));
		}
		if(province_code.length() > 0){
			this.request.setAttribute("cityList", new CityBiz().getCityByProvinceCode(province_code));
		}
		
		CommonBean rtmrole = new CommonBiz().getCommonByTypeAndCode("role", "RTM");
		CommonBean shiyebu = new CommonBiz().getCommonByTypeAndCode("shiyebu", "ITMall");
		CommonBean shiyeburole = new CommonBiz().getCommonByTypeAndCode("role", "shiyebu");
		
		
		BaoxiaoBean bean = baoxiaoBiz.getBaoxiaoBaseInfoFromEmp(userLogin.getLoginId());
		if(role.equals(rtmrole.getCommonValue()) && userLogin.getShiyebu().equals(shiyebu.getCommonValue())){

			PageBean res = baoxiaoShenpiBiz.searchBaoxiaoShenpiHistory(area_code,province_code,city_code, userLogin.getLoginId(), bean.getCtm_name(), ctm_code,ctm_name, baoxiao_state, baoxiao_starttime, baoxiao_endtime,false, curpage);
			this.request.setAttribute("pageBean", res);
			this.request.setAttribute("isZongbu", "");
			this.request.setAttribute("rtm_code", bean.getCtm_code());
			this.request.setAttribute("rtm_name", bean.getCtm_name());
			result = "success";
			return result;
		}else if(role.equals("1") || role.equals("zongbu") || (role.equals(shiyeburole.getCommonValue())) && userLogin.getShiyebu().equals(shiyebu.getCommonValue())){

			PageBean res = baoxiaoShenpiBiz.searchBaoxiaoShenpiHistory(area_code,province_code,city_code, "", rtm_name, ctm_code,ctm_name, baoxiao_state, baoxiao_starttime, baoxiao_endtime,true, curpage);
			this.request.setAttribute("pageBean", res);
			this.request.setAttribute("isZongbu", "true");
		}else{
			//PageBean res = baoxiaoShenpiBiz.searchCTMBaoxiao( bean.getCtm_code(),"","","", 1);
			 message = "您没有权限执行该操作！";
			this.request.setAttribute("message", message);
			return "error";
		}
		return "success";
	}

}
